package greed;

/**
 * @Classname : Candy
 * @Description : 135. 分发糖果
 * https://leetcode.cn/problems/candy/
 * @Author : chentianyu
 * @Date 2023/2/24 00:04
 */


public class Candy {
    public int candy(int[] ratings) {
        if (ratings.length == 1) return 1;
        int ans = 0;
        int[] asign = new int[ratings.length];
        for (int i=0; i<ratings.length-1; i++) {
            if (asign[i] == 0) asign[i]++;
            if (ratings[i] < ratings[i+1]) asign[i+1] = Math.max(asign[i]+1, asign[i+1]);
        }
        for (int i=ratings.length-1; i>0; i--) {
            if (asign[i] == 0) asign[i]++;
            if (ratings[i] < ratings[i-1]) asign[i-1] = Math.max(asign[i]+1, asign[i-1]);
        }
        for (int v : asign) ans += v;
        return ans;
    }
}
